function add(a, b) {
  return a + b;
}

function square(a) {
  return a * a;
}

function plusOne(c) {
  return c + 1;
}

// const pipe = (...args) => {
//   return (...arguments) => {
//     const init = args[0].apply(null, arguments);
//     return args.slice(1).reduce((memo, current) => {
//       return current(memo);
//     }, init);
//   };
// };

const pipe = (...args) => {
  //接收多个函数
  return (...arguments) => {
    return args.reduce((memo, current) => {
      //第一个参数是前一个执行的返回值
      //第二个参数是当前的函数
      //把当前函数的执行返回值给下一个函数
      //reduce过程中不停的执行
      return current(
        typeof memo === "function" ? memo.apply(memo, arguments) : memo
      );
    });
  };
};

const compose = (...args) => {
  return (...arguments) => {
    return args.reduceRight((memo, current) => {
      return current(
        typeof memo === "function" ? memo.apply(memo, arguments) : memo
      );
    });
  };
};

var addSquareAndPlusOneCompose = compose(plusOne, square, add);

var addSquareAndPlusOne = pipe(add, square, plusOne);

// function composite(add, square, plusOne) {
//   return function () {};
// }

// console.log(addSquareAndPlusOne(1, 2));
console.log(addSquareAndPlusOneCompose(1, 2));
